package com.smart.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smart.dao.BaseDao;
import com.smart.dao.InfoDao;
import com.smart.dao.Page;
import com.smart.dao.PageRequest;
import com.smart.model.Info;
import com.smart.model.InfoCond;
import com.smart.model.InfoViewData;

/**
 * 
 * @author Sunxin
 *
 */
 
@Service
public class InfoServiceImpl extends BaseEntityService<Info, InfoCond, Long> implements InfoService{

	@Autowired
	private InfoDao dao;
	
	@Override
	protected BaseDao<Info, InfoCond, Long> getDao() {
		return dao;
	}
	
	public Page<Info> findAll(PageRequest pageRequest, InfoCond cond) {
		return super.findAll(pageRequest, cond);
	}

	@Override
	public Info findOneNext(Long id) {
		InfoCond cond = new InfoCond();
		cond.setIdGt(id);
		return super.findOne(cond);
	}

	@Override
	public Info findOnePrev(Long id) {
		InfoCond cond = new InfoCond();
		cond.setIdLe(id);
		return super.findOne(cond);
	}

	@Override
	public InfoViewData getDataForInfoView(Long id) {
		InfoViewData data = new InfoViewData();
		data.setInfo(this.findOne(id));
		data.setPrev(this.findOnePrev(id));
		data.setNext(this.findOneNext(id));
		InfoCond cond = new InfoCond();
		cond.setType(data.getInfo().getType());
		cond.addOrderByDesc("crtTime");
		PageRequest top10PageReq = new PageRequest();
		top10PageReq.setPageSize(10);
		Page<Info> top10 = this.findAll(top10PageReq, cond);
		data.setTop10(top10.getContent());
		return data;
	}

	@Override
	public void updateViewCount(Long id) {
		dao.updateViewCount(id);
	}
	
}
